//
//  ViewController.m
//  ProjectsOne
//
//  Created by iOS开发 on 2018/7/9.
//  Copyright © 2018年 iOS开发. All rights reserved.
//

#import "ViewController.h"
#import "SegView.h"
#import "NewsCollectionCell.h"
#import "NewsModel.h"
#import "ContentInfoVC.h"
#import "SearchVC.h"

@interface ViewController ()<SegViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) SegView *seg;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) UICollectionView *collection;

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation ViewController

#pragma mark - init
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
        // 初始化 给10条数据
        for (NSString *str in _titleArr) {
            NSMutableArray *tempArr = [NSMutableArray array];
            [self arrayAddTenData:tempArr];
            [_dataArr addObject:tempArr];
        }
    }
    return _dataArr;
}

- (NSArray *)titleArr {
    if (!_titleArr) {
        _titleArr = @[@"新闻" , @"体育"];
        
    }
    return _titleArr;
}

- (UICollectionView *)collection {
    if (!_collection) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT - kCustomNav - 41);
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 41 + kCustomNav, WIDTH, HEIGHT - kCustomNav - 41) collectionViewLayout:flowLayout];
        _collection.delegate = self;
        _collection.dataSource = self;
        _collection.showsVerticalScrollIndicator = NO;
        _collection.showsHorizontalScrollIndicator = NO;
        _collection.pagingEnabled = YES;
        _collection.backgroundColor = [UIColor whiteColor];
        
        [_collection registerClass:[NewsCollectionCell class] forCellWithReuseIdentifier:@"mainCell"];
    }
    return _collection;
}

- (SegView *)seg {
    if (!_seg) {
        _seg = [[SegView alloc] initWithFrame:CGRectMake(0, kCustomNav, WIDTH, 41) Title:self.titleArr];
        _seg.delegate = self;
        _seg.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:249 / 255.0 blue:249 / 255.0 alpha:1];
    }
    return _seg;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self setNav];
}

- (void)setNav {
    // 标题
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.titleArr.firstObject;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"ffffff"], NSFontAttributeName: [UIFont boldSystemFontOfSize:19]}];
    
    // 搜索
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    [searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    searchButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [searchButton addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
    searchButton.frame = CGRectMake(0, 0, 40, 40);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];
    
    [self.view addSubview:self.collection];
    [self.view addSubview:self.seg];
}

#pragma mark - tableViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NewsCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mainCell" forIndexPath:indexPath];
    
    [cell takeData:self.dataArr[indexPath.row]];
    [self refreshBlock:cell index:indexPath.row];
    return cell;
}



#pragma mark - refresh
- (void)refreshBlock:(NewsCollectionCell *)cell index:(NSInteger)idx {
    // 刷新 每次刷新增加10条数据
    @weakify(self);
    cell.headerBlock = ^NSArray *{
        @strongify(self);
        [self.dataArr[idx] removeAllObjects];
        [self arrayAddTenData:self.dataArr[idx]];
        return self.dataArr[idx];
    };
    cell.footerBlock = ^NSArray *(NSInteger page) {
        @strongify(self);
        [self arrayAddTenData:self.dataArr[idx]];
        return self.dataArr[idx];
    };
    
    // 点击cell
    cell.block = ^(NSInteger indexRow) {
        @strongify(self);
        ContentInfoVC *infoVC = [[ContentInfoVC alloc] init];
        infoVC.model = self.dataArr[idx][indexRow];
        [self.navigationController pushViewController:infoVC animated:YES];
    };
}



#pragma mark - seg
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger round = roundf(scrollView.contentOffset.x / WIDTH);
    [self clickSegIndex:round];
    [_seg segAction:round];
}
// 点击切换模块
- (void)clickSegIndex:(NSInteger)index {
    self.navigationItem.title = self.titleArr[index];
    [_collection scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

// 跳转搜索
- (void)search:(UIButton *)button {
    SearchVC *vc = [[SearchVC alloc] init];
    UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:navc animated:YES completion:nil];
}

// 添加数据
- (void)arrayAddTenData:(NSMutableArray *)arr {
    for (NSInteger i = 0; i < 10; i++) {
        [arr addObject:[[NewsModel alloc] init]];
    }
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationController.navigationBar.translucent = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
